layui.use(['grid', 'layarea', 'laydate'], function () {
    var pager = layui.grid
        , layarea = layui.layarea
        , laydate = layui.laydate
        , tableId = "drug-table"
        , tableFilter = "drug-table-filter"
        , searchFilter = "drug-search"
        , cols = [
            {field: "_serial", title: "序号"},
            {field: "name", title: "产品名称"},
            {field: "company", title: "公司名称"},
            {field: "desc", title: "风险描述"},
            {field: "analysis", title: "风险分析"},
            {field: "reported_at", title: "上报时间"},
            {field: "unit", title: "上报单位"},
            {field: "address", title: "所属地区"},
            {field: "phone", title: "联系电话"},
            {field: "checked_at", title: "处理时间"},
            {field: "status", title: "处理状态"},
            {field: "comment", title: "风险评价"},
        ]
        , table = {
            "id": tableId,
            "elem": "#" + tableId,
            "limit": 8,
            "limits": [10, 20, 50, 100],
            "cols": [cols],
            "method": "post",
            "request": {"pageName": "page", "limitName": "per-page"},
            "url": "/report-history-list",
            "toolbar": "#toolBar",
            "page": {"hash": "fenye"},
            "autoSort": false,
            "defaultToolbar": ["filter", "print", {
                "title": "导出Excel",
                "layEvent": "OUTPUT_EXCEL",
                "icon": "layui-icon-export"
            }]
        }
        , eventOptions = {
            "watch": {"url": "/report-watch"},
            "unwatch": {"url": "/report-unwatch"},
        }
        , events = {
            "watch": function (obj, config, that) {
                var helper = layui.helper, $ax = layui.ax, layer = layui.layer
                layer.prompt({
                    formType: 2, //输入框类型，支持0（文本）默认1（密码）2（多行文本）
                    value: '无', //初始时的值，默认空字符
                    maxlength: 500, //可输入文本的最大长度，默认500
                    title: '请输入风险评估'
                }, function (value, index, elem) {
                    var url = config.url + '?' + helper.urlEncode({id: obj.data['id']});
                    var ajax = new $ax(url, function (res) {
                        that.reloadTable(null, null, true);
                        layer.close(index)
                    });
                    ajax.setMethod('post')
                    ajax.setData({comment: value});
                    ajax.send();
                });
            },
            "unwatch": function (obj, config, that) {
                var $ax = layui.ax, helper = layui.helper
                layer.alert("确定忽略该报告？", function (index) {
                    var ajax = new $ax(config.url + '?' + helper.urlEncode({id: obj.data['id']}));
                    ajax.setMethod('post');
                    ajax.setResult(function (result) {
                        that.reloadTable(null, null, true);
                        layer.close(index);
                        layer.msg(result.msg)
                    });
                    ajax.send();
                })
            }
        }

    layarea.render({
        elem: '#'+ searchFilter,
        change: function (res) {
            //选择结果
            console.log(res)
        }
    });

    laydate.render({
        elem: '#yak_dt_s'
    });

    laydate.render({
        elem: '#yak_dt_e'
    });

    pager.render({
        "searchFilter": searchFilter,
        "tableId": tableId,
        "tableFilter": tableFilter,
        "sort": "server",
        "countCache": true,
        "table": table,
        "editor": {},
        "eventOptions": eventOptions
    }, events)
})
